package com.szy.web.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.szy.web.dao.NewsDAO;
import com.szy.web.model.Page;

public class ShowNewsServlet extends HttpServlet implements Servlet {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	private Page page;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
	   if ( request.getSession().getAttribute("Login") != null) 
	   {
		  
			String message = (String) request.getSession().getAttribute("Login");
			if (!message.equals("true"))
			{
				 //如果登录标志不为true则跳转回登录页面
				 response.sendRedirect("index.jsp");
				 return;
			}
			else
			{
				request.getSession().setAttribute("Login","true");
			}   
	   }
	   else
	   {
	    	response.sendRedirect("index.jsp");
	    	return;
	   }
	   
	   
		String method = request.getParameter("method");
		if (method.equalsIgnoreCase("view")) {
			String page = request.getParameter("currentPage");
			
			int currentPage = 1;
			int pageSize = 10;
			if (page != null) 
			{
				currentPage = Integer.parseInt(page);
				
			}
			NewsDAO newsDAO;
			try {
				newsDAO = new NewsDAO();
				Page pageModel = newsDAO.showNews(currentPage, pageSize);// 查询新闻从currPage到currPage+pageSize
				request.setAttribute("pageModel", pageModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				request.getRequestDispatcher("showNews.jsp").forward(request,response);
			} catch (ClassNotFoundException e) {
				
				e.printStackTrace();
			}
			
		}
		else if (method.equalsIgnoreCase("details")) 
		{
			int nid=Integer.parseInt(request.getParameter("nid"));
			NewsDAO newsDAOtmp;
			try {
				newsDAOtmp = new NewsDAO();
				Page detailsModel=newsDAOtmp.showNews(nid);
				request.setAttribute("detailsModel", detailsModel);
				request.getSession ( ).setAttribute ( "Login", "true" );
				request.getRequestDispatcher("newsDetails.jsp").forward(request,response);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}

		}
//		else if (method.equalsIgnoreCase("newsdelete")) 
//		{
//			int nid=Integer.parseInt(request.getParameter("nid"));
//			NewsDAO newsDAOtmp;
//			try {
//				newsDAOtmp = new NewsDAO();
//				Page detailsModel=newsDAOtmp.showNews(nid);
//				request.setAttribute("detailsModel", detailsModel);
//				request.getSession ( ).setAttribute ( "Login", "true" );
//				request.getRequestDispatcher("newsDetails.jsp").forward(request,response);
//			} catch (ClassNotFoundException e) {
//				e.printStackTrace();
//			}
//
//		}
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
